package org.web.demo.servlet;


import lombok.extern.slf4j.Slf4j;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author yexi
 */
@WebServlet("/forward")
@Slf4j
public class ForwardServlet extends HttpServlet {

    public ForwardServlet() {
        log.info("ForwardServlet实例化");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        log.info("ForwardServlet处理请求");
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/WEB-INF/views/dispatcher.jsp");
        resp.getWriter().println("使用forward转发之前所做的操作是无效的");
        requestDispatcher.forward(req, resp);
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().println("使用forward转发后续的操作也是无效的");
    }

    @Override
    public void destroy() {
        log.info("ForwardServlet对象销毁");
    }

    @Override
    public void init() throws ServletException {
        log.info("ForwardServlet属性初始化");
    }
}
